package io.renren.modules.app.controller;

import com.zheok.testing.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import utils.R;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.UUID;


/**
 * 进场记录表
 *
 * @author liliang
 * @email 2863751700@qq.com
 * @date 2022-01-21 15:27:42
 */
@Api(tags = "文件操作接口")
@RestController
@RequestMapping("testing/file")

public class FileController {
    @Value("${uploadPath}")
    private String uploadPath;
    @Value("${viewPath}")
    private String viewPath;
    @ApiOperation(value = "参数名file 上传文件接口、返回的是文件地址")
    @PostMapping("uploadFile")
    @ApiResponses({@ApiResponse(code=0,message = "ok",response = UploadVo.class)})
    public R uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
        //判断文件是否为空
        if (Objects.requireNonNull(file.getOriginalFilename()).isEmpty()) {
            //跑出异常
            throw new RuntimeException("上传文件为空!");
        }
        String fileName = System.currentTimeMillis() + UUID.randomUUID().toString() + file.getOriginalFilename();
        //上传文件到服务器
        File saveFile = new File(uploadPath,fileName);
        file.transferTo(saveFile);
        UploadVo uploadVo = new UploadVo();
        uploadVo.setFileUrl(viewPath+fileName);
        uploadVo.setFilePath(uploadPath+fileName);
        return R.ok().put("data",uploadVo);
    }
    @ApiOperation(value = "参数名file 上传文件接口、返回的是文件地址")
    @PostMapping("uploadedit")
    @ApiResponses({@ApiResponse(code=0,message = "ok",response = UploadVo.class)})
    public WangEditRes uploadedit(@RequestParam("file") MultipartFile file) throws IOException {
        //判断文件是否为空
        if (Objects.requireNonNull(file.getOriginalFilename()).isEmpty()) {
            //跑出异常
            throw new RuntimeException("上传文件为空!");
        }
        String fileName = System.currentTimeMillis() + UUID.randomUUID().toString() + file.getOriginalFilename();
        //上传文件到服务器
        File saveFile = new File(uploadPath,fileName);
        file.transferTo(saveFile);
        ArrayList<String> sb = new ArrayList<>();
        sb.add(viewPath+fileName);
        return new WangEditRes(0,sb);
    }


}
